package com.eff.android.lib.graphics;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

public class Base64Convert {
	private static final String TAG = "Base64Convert";
	
	public Base64Convert(){
		//TODO: Any things;
	}
	
	//Decode base64 data
	//	input: Encode source
	//	option: Optional decode process
	//			always: Base64.DEFAULT
	public byte[] Decode(String input, int option)
	{
		//Return byte array data
		byte[] output = null;
		
		if(input.isEmpty()){
			Log.e(TAG, "Input data isn't empty.\n");
			return output;
		}
		
		//Get image data base64 from string value
		//23: data:image\/jpeg;base64,<Image data>
		input = input.substring(23);
		
		//Decode from base64 data
		try{
			//decode data with option
			output = Base64.decode(input, option); 
		}catch(IllegalArgumentException ex){
			Log.e(TAG, ex.getMessage());
		}
		
		if(output == null){
			Log.e(TAG, "Can't decode input data.\n");
		}
		
		return output;
	}
	
	//Decompress from byte array data
	//	input: Byte array data
	public Bitmap Decompress(byte[] input)
	{
		//return bitmap data
		Bitmap output = null;
		
		if(input == null){
			Log.e(TAG, "Input data isn't empty.\n");
			return null;
		}
		
		output = BitmapFactory.decodeByteArray(input, 0, input.length);
		
		if(output == null){
			Log.e(TAG, "Can't decompress input data.\n");
		}
		
		return output;
	}
	
	//Decode and Decompress base 64 data
	//	input: Encode source
	//	option: Optional decode process
	//			always: Base64.DEFAULT
	public Bitmap Decompress(String input, int option)
	{
		Bitmap output = null;
		
		byte[] bytes = Decode(input, option);
		if(bytes != null){
			output = Decompress(bytes);
		}
		
		return output;
	}
}
